package core.util;

/**
 * @author songduk park
 * 
 */
public class Period {
	public int this_year = 0;
	public int this_month = 0;
	public int this_week = 0;

	public int this_year1 = 0;
	public int this_month1 = 0;
	public int this_week1 = 0;

	public String type_period;

	public final String lable_thie_year;
	public final String lable_thie_month;
	public final String lable_thie_week;

	public final String lable_thie_yearweek;
	public final String lable_thie_yearmonth;

	public final String lable_type_period;

	public final String lable_period;

	public int period_before = 0;
	public int period_before1 = 0;

	public Period() {
		this("");
	}

	public Period(String suffix) {
		lable_thie_year = "this_year" + suffix;
		lable_thie_month = "this_month" + suffix;
		lable_thie_week = "this_week" + suffix;

		lable_type_period = "type_period" + suffix;

		lable_thie_yearweek = "yearweek" + suffix;
		lable_thie_yearmonth = "yearmonth" + suffix;

		lable_period = "pos_period" + suffix;
	}

	public void putToMap(StringHashMap map) {
		map.put(lable_thie_year, this_year);

		if (type_period.equals("M")) {
			map.put(lable_thie_month, this_month);
			map.put(lable_thie_yearmonth, this_year * 100 + this_month);
		} else {
			map.put(lable_thie_week, this_week);
			map.put(lable_thie_yearweek, this_year * 100 + this_week);

		}
	}

	public void print() {
		System.out.println("this_year= " + this.this_year);
		System.out.println("this_month= " + this.this_month);
		System.out.println("this_week= " + this.this_week);

		System.out.println();
		System.out.println("this_year1= " + this.this_year1);
		System.out.println("this_month1= " + this.this_month1);
		System.out.println("this_week1= " + this.this_week1);
		System.out.println();

		System.out.println("period_before=" + period_before);
		System.out.println("period_before1=" + period_before1);

	}

	public void setCurrentPeriod(StringHashMap map, int comparing_period) {

		this.this_year = map.getIntValue("this_year");
		this.this_month = map.getIntValue("this_month");
		this.this_week = map.getIntValue("this_week");

		this.this_year1 = map.getIntValue("this_year1");
		this.this_month1 = map.getIntValue("this_month1");
		this.this_week1 = map.getIntValue("this_week1");

		int multiple = 0;
		int now1;
		int now;

		Object temp_this_month = map.get("this_month");

		if (comparing_period == 0) // a year before
		{
			if (temp_this_month != null) {
				this.period_before = (this.this_year - 1) * 100
						+ this.this_month;
				this.period_before1 = (this.this_year1 - 1) * 100
						+ this.this_month1;
			} else {
				this.period_before = (this.this_year - 1) * 100
						+ this.this_week;
				this.period_before1 = (this.this_year1 - 1) * 100
						+ this.this_week1;
			}
		} else // the same period before
		{
			if (temp_this_month != null) {
				multiple = 12;

				now1 = this_year1 * multiple + this_month1;
				now = this_year * multiple + this_month;

			} else {
				multiple = 52;

				now1 = this_year1 * multiple + this_week1;
				now = this_year * multiple + this_week;
			}

			int duration = now1 - now;
			int before1 = now - 1;
			int before = before1 - duration;

			this.period_before = (before / multiple) * 100
					+ (before % multiple);
			this.period_before1 = (before1 / multiple) * 100
					+ (before1 % multiple);
		}
		print();		
		map.put("period_before", period_before);
		map.put("period_before1", period_before1);
	}
}
